"""
WebSocket事件处理模块
"""
from flask_socketio import emit
from services.system_service import system_service

def register_socket_handlers(socketio):
    """注册WebSocket事件处理器"""
    
    @socketio.on('connect')
    def handle_connect():
        """WebSocket连接处理"""
        print('Client connected')
        emit('system_status', system_service.get_status())

    @socketio.on('disconnect')
    def handle_disconnect():
        """WebSocket断开处理"""
        print('Client disconnected')

    @socketio.on('voice_command')
    def handle_voice_command(data):
        """处理语音指令"""
        command = data.get('command', '').lower()
        
        if command == 'start' or command == '开始':
            if not system_service.is_running():
                system_service.start_system()
                socketio.emit('system_status_changed', {'running': True})
                emit('voice_response', {'message': 'System started by voice command'})
            else:
                emit('voice_response', {'message': 'System is already running'})
                
        elif command == 'stop' or command == '结束':
            if system_service.is_running():
                system_service.stop_system()
                socketio.emit('system_status_changed', {'running': False})
                emit('voice_response', {'message': 'System stopped by voice command'})
            else:
                emit('voice_response', {'message': 'System is not running'}) 